package com.example.houtai.aop;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.houtai.config.Logger;
import com.example.houtai.entity.SysLog;
import com.example.houtai.service.ISysLogService;
import com.org.dto.LogDto;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.awt.event.MouseEvent;
import java.lang.reflect.Method;
import java.net.URL;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Date;

/**
 *
 */
@Aspect
@Component
@Slf4j
public class AopLog {

    /*@Autowired
    private AmqpTemplate template;*/
    @Autowired
    private RabbitTemplate rabbitTemplate;

    private long time;

    @Pointcut("execution(* com.example.houtai.controller.*.*(..))")
    public void myPointcut() {

    }

    @Before("myPointcut()")
    public void before(JoinPoint joinPoint) {

        time = System.currentTimeMillis();
    }

    @AfterReturning(pointcut = "myPointcut()")
    public void after(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Logger annotation = method.getAnnotation(Logger.class);
        if (annotation == null) {
            //如果无注解,跳过
            return;
        }
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        //SysLog sysLog = new SysLog();
        LogDto sysLog = new LogDto();
        //用户名称
        sysLog.setUsername("admin");
        //操作内容
        sysLog.setOperation(annotation.value());
        //操作方法
        //logDTO.setMethod(request.getRequestURI());

        //操作时间
        /*sysLog.setCreateDate(LocalDate.now());*/
        //方法参数
        Object[] args = joinPoint.getArgs();
        String s = Arrays.toString(args);
        sysLog.setParams(s + "");
        //方法执行时间
        sysLog.setTime(System.currentTimeMillis() - time);
        //ip端口
        sysLog.setIp(request.getLocalAddr() + "");

        //请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        sysLog.setMethod(className + "." + methodName + "()");
        //指定交换机,指定路由建
        rabbitTemplate.convertAndSend("mylog","log.one", JSON.toJSONString(sysLog));
        log.info("-------进入添加日志-----------");

    }

}
